<?PHP
global $conf;

	$purchasable=false;
	$conf->style['masthead']="Purchase";
if(!$c_content && $conf->qm[0]=='buy')
{
		$c_content=new content;
		$c_content->where['id']=$conf->qm[1];
		$c_content->get_content();
		
		
		if(!empty($c_content->data))
		{
			
			$c_content->get_content_addons();
			
			if($c_content->data['access_enabled']=='enabled')
			{
					$access=new content_access();
					$access->mode='silent';
					$access->where['content']=$c_content->data['id'];
					$_SESSION['content']=$c_content->data['id'];
					$access->check_token();
					
					if(!$access->access)
					{
							$purchasable=true;
							
							$_SESSION['item_cost']=$c_content->data['access_cost'];
							$transaction['item-name']=$c_content->data['title'];
							$transaction['item-cost']=$c_content->data['access_cost'];
							$tfp=$conf->a['transaction-fee-percent']/100;
							$tic=$transaction['item-cost'];
							$transaction['transaction-cost-percent']=$tfp*$tic;
							$transaction['transaction-cost-amount']=$conf->a['transaction-fee-amount'];
													$transaction['transaction-cost-total']=$transaction['transaction-cost-amount']+$transaction['transaction-cost-percent'];	
							$transaction['payment-amount']=$transaction['item-cost']+$transaction['transaction-cost-total'];
							//round everything up
							//round(1.95583, 2);
							$transaction['transaction-cost-total']=round($transaction['transaction-cost-total'], 2);
							$transaction['payment-amount']=round($transaction['payment-amount'], 2);
							$_SESSION["Payment_Amount"]=$transaction['payment-amount'];
							
					}
			
			}
		}
		$content=$c_content->data;
	
}
if($_SESSION['payment_method']=='CC' && $_POST['credit_card_processed']=='Y')
{
	$purchasable=true;
}
if($purchasable)
{
	
	include 'admin/m/payment/paypalfunctions.php';

	$access_data=new content_access();
	$access_data->mode='silent';
	$access_data->where['id']=$c_content->data['id'];
	$access_data->check_permissions();
	$ad=current(current($access_data));


	if($conf->qm[2]=='user')
	{
		if($_POST['terms_check_check']==1)
		{
			$_SESSION['transstatus']='user';
		}
		else
		{
			$_SESSION['transstatus']='terms';
		}
		
	}

	else if($conf->qm[2]=='pay')
	{

		//$payment_method=$_POST['payment_method'];
		if($_SESSION['transstatus']=='user' && !empty($_POST['payment_method']))
		{ 
	
				$_SESSION['payment_method']=$_POST['payment_method'];
				$_SESSION['transstatus']='pay';
				
				if($_SESSION['user_level']==0)
				{
					$_SESSION['ud']=$_POST;
				}
				else
				{
					$_SESSION['ud']=$_SESSION['user_data'];
				}
				
		}
		else
		{	//echo 'aow';
			
			$_SESSION['transstatus']='user';
		}
		
	}
	else if($conf->qm[2]=='review')
	{

		//$payment_method=$_POST['payment_method'];
		if($_SESSION['transstatus']=='pay' && isset($_REQUEST['token']))
		{ 
			$_SESSION['transstatus']='review';
		}
		else
		{	
			
			$_SESSION['transstatus']='pay';
		}
		
	}
	else if($conf->qm[2]=='confirm')
	{	
		if($_SESSION['payment_method']=='PayPal')
		{
			include 'admin/m/payment/paypal_confirm.php';
		}
		else if($_SESSION['payment_method']=='CC')
		{
			include 'admin/m/payment/2co_confirm.php';
		
			
		}
	
		
		if($_SESSION['transstatus']=='review' && $_POST['pay']=='yes' && $payment_success)
		{
			
			$_SESSION['transstatus']='confirm';
		}
		else if($_SESSION['payment_method']=='CC' && $payment_success)
		{
				$conf->messages='Loading 2co';
			$_SESSION['transstatus']='confirm';
		}
		else
		{
				$conf->messages='Payment Failed';
			$_SESSION['transstatus']='review';
		}
		
	}
	else if($ad['access_terms']=='true')
	{
		//echo 'terms';
		$_SESSION['transstatus']='terms';
	}
	
	
	if($_SESSION['transstatus']=='terms')
	{
				unset($_SESSION['ud']);
				$_SESSION['transid']=$conf->a['cc-prehash'].'-'.randomchar(10,true);
				$trans_content = new content();
				$trans_content->where['id']=$ad['access_terms_content'];
				$trans_content->get_content();
        
				$terms_content=current($trans_content->data);
				$conf->page_render='admin/m/buy/o_terms.php';
				$crumbs='Terms and Conditions';
				$view_buy=true;
				$next_page=$conf->absolute_path.'buy/'.$c_content->data['id'].'/user';
		
	}
	
	if($_SESSION['transstatus']=='user')
	{
				$conf->page_render='admin/m/buy/o_buy.php';
				$crumbs='User Information';
				$view_buy=true;
				if($_SESSION['user_level']==0)
				{
					$buy='new';
				}
				$next_page=$conf->absolute_path.'buy/'.$c_content->data['id'].'/pay';
		
	}
	
	if($_SESSION['transstatus']=='pay')
	{
		//process Paypal Login
	$_SESSION['user_data']=$_POST;
				if($_SESSION['payment_method']=='PayPal')
				{
					include 'admin/m/payment/paypal.php';
				}
				else if($_SESSION['payment_method']=='CC')
				{
					$conf->page_render='admin/m/payment/o_cc.php';
					$view_buy=true;
				}
			
				$crumbs='Redirecting';
	
	}
	
	if($_SESSION['transstatus']=='review')
	{
	
				$crumbs='Review your Order';
				include 'admin/m/payment/paypal_review.php';	
				$conf->page_render='admin/m/buy/o_review.php';
				$view_buy=true;

	
	}
	
	if($_SESSION['transstatus']=='confirm')
	{
				if($_SESSION['payment_method']=='PayPal')
				{
					$_SESSION['transstatus']='none';
					unset($_SESSION['ud']);
					unset($_SESSION['transid']);
					$buy = new buy;
					$buy->input['transaction_details']=$resArray;
					$buy->input['content']=$content['id'];
					//$buy->process();
					$conf->page_render='admin/m/buy/o_confirm.php';
					$view_buy=true;
				}
				else if($_SESSION['payment_method']=='CC')
				{
					
				
				
					$buy = new buy;
					$buy->input['transaction_details']=$_POST;
					$buy->input['content']=$content['id'];
					$buy->process_2co();
					$conf->page_render='admin/m/buy/o_confirm.php';
					$view_buy=true;
						$_SESSION['transstatus']='none';
						unset($_SESSION['ud']);
						unset($_SESSION['user_data']);
						unset($_SESSION['transid']);
				}
				

	
	}
	
}

if($view_buy)
{
	
}
else
{
		$home=true;
		$conf->set_default_q();
}

?>